package main

import "fmt"

// 1.定义接口
type USB interface {
	start() //USB设备开始工作
	end()   //USB设备结束工作
}

// 2.实现类
type Mouse struct {
	name string
}

type FlashDisk struct {
	name string
}

func (m Mouse) start() {
	fmt.Println("开始")

}
func (m Mouse) end() {
	fmt.Println("结束")

}
func (f FlashDisk) start() {
	fmt.Println("开始")

}
func (f FlashDisk) end() {
	fmt.Println("结束")

}
func testInterface(usb USB) { //usb =m1 usb = f1
	usb.start()
	usb.end()
}

func myprint(s string) { fmt.Println(s) }

func funA() {
	fmt.Println("我是函数funA()....")
}

func funB() {
	fmt.Println("我是函数funB()..")
	for i := range 5 {
		if i == 3 {
			panic("panic!!!!")
		}

	}
	defer myprint("defer funB(): 1.....")
	defer myprint("defer funB():2.....")
}

type Person struct {
	name string
	age  int
}
type Any interface{}

func main() {
	//m1 := Mouse{name: "罗技"}
	//fmt.Println(m1.name)
	//f1 := FlashDisk{name: "三星64G"}
	//fmt.Println(f1.name)
	//testInterface(m1)
	//testInterface(f1)
	//funA()
	//defer func() {
	//	if msg := recover(); msg != nil {
	//		fmt.Println("recover")
	//	}
	//}()
	//defer myprint("defer main: 3.....")
	//funB()
	//defer myprint("defer main: 4.....")
	//fmt.Println("main..over。。。。")

	//可以给一个空接口类型的变量 var val interface {} 赋任何类型的值。空接口类似于java中所有类的父类object
	var val Any
	val = 5
	fmt.Printf("val has the value: %v\n", val)
	val = "ABC"
	fmt.Printf("val has the value: %v\n", val)
	pers1 := new(Person)
	pers1.name = "Rob Pike"
	pers1.age = 55
	val = pers1
	fmt.Printf("val has the value: %v\n", val)
	switch t := val.(type) {
	case int:
		fmt.Printf("Type int %T\n", t)
	case string:
		fmt.Printf("Type string %T\n", t)
	case bool:
		fmt.Printf("Type boolean %T\n", t)
	case *Person:
		fmt.Printf("Type pointer to Person %T\n", t)
	default:
		fmt.Printf("Unexpected type %T", t)
	}
}
